package com.bto.leasetest;


import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;

/**
 * @author 王小波
 * @description:
 * @date 2023/9/4 8:49
 */
public class CodeGen {

    public static final String[] tables = {"lease_review_test", "lease_review_config"};

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://192.168.150.250:3306/lease-test?characterEncoding=utf-8");
        dataSource.setUsername("admin");
        dataSource.setPassword("bto123");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        //GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.setBasePackage("com.bto.leasetest");
        globalConfig.getEntityConfig().setOverwriteEnable(true);


        //设置表前缀和只生成哪些表
//        globalConfig.setTablePrefix("sto_");
        globalConfig.setGenerateTable(tables);

        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);

        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);

        globalConfig.enableController();

        globalConfig.enableService();
        globalConfig.getPackageConfig().setServicePackage("com.bto.leasetest.dao");

        globalConfig.enableServiceImpl();
        globalConfig.getPackageConfig().setServiceImplPackage("com.bto.leasetest.dao.imp");

        globalConfig.enableTableDef();


        //可以单独配置某个列
        ColumnConfig createColumnConfig = new ColumnConfig();
        createColumnConfig.setColumnName("create_time");
        createColumnConfig.setOnInsertValue("now()");
        createColumnConfig.setPropertyType("java.util.Date");

        ColumnConfig updateColumnConfig = new ColumnConfig();
        updateColumnConfig.setColumnName("update_time");
        updateColumnConfig.setPropertyType("java.util.Date");
        updateColumnConfig.setOnUpdateValue("now()");
//        columnConfig.setLarge(true);
//        columnConfig.setVersion(true);
//        globalConfig.setColumnConfig("*", columnConfig);
        globalConfig.getStrategyConfig().setColumnConfig(createColumnConfig);
        globalConfig.getStrategyConfig().setColumnConfig(updateColumnConfig);
        return globalConfig;
    }

    public static GlobalConfig createGlobalConfigUseStyle2() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("com.test");

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix("tb_")
                .setGenerateTable("tb_account", "tb_account_session");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true);

        //设置生成 mapper
        globalConfig.enableMapper();

        //可以单独配置某个列
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
        globalConfig.getStrategyConfig()
                .setColumnConfig("tb_account", columnConfig);

        return globalConfig;
    }


//    public static void jsonTypeColum(String tableName , String columnName , Class<? extends BaseJsonTypeHandler> typeHandler  , GlobalConfig globalConfig){
//        String proType = "com.alibaba.fastjson2.JSONObject";
//        if(typeHandler == MybatisFlexJsonArrayTypeHandler.class){
//            proType = "com.alibaba.fastjson2.JSONArray";
//        }
//        ColumnConfig jsonColumnConfig= new ColumnConfig();
//        jsonColumnConfig.setColumnName(columnName);
//        jsonColumnConfig.setPropertyType(proType);
//        jsonColumnConfig.setTypeHandler(typeHandler);
//
//        globalConfig.getStrategyConfig().setColumnConfig(tableName , jsonColumnConfig);
//    }
}